/**
 * MonitorServiceImpl.java
 * Created at 2018-1-4
 * Created by zhanghailin
 * Copyright (C) 2018 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.app.monitor.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.app.monitor.service.MonitorService;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.rms.monitor.dao.ITtPackDao;
import com.broadtext.rms.monitor.model.TtCellvoltageCurrent;
import com.broadtext.rms.monitor.model.TtPack;
import com.broadtext.rms.monitor.model.TtPackCurrent;
import com.broadtext.rms.monitor.model.TtSamplingpointTempCurrent;

/**
 * <p>ClassName: MonitorServiceImpl</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2018-1-4</p>
 */
@Service("monitorService")
public class MonitorServiceImpl extends EntityServiceImpl<TtPack> implements MonitorService{
    
    /**
     * 注入电池监控Dao
     */
    @Autowired
    @Qualifier("ttPackDao")
    private ITtPackDao ttPackDao;
    
    @Autowired
    @Qualifier("ttPackDao")
    public void setEntityDao(EntityDao<TtPack> entityDao) {
        super.entityDao = entityDao;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getMonitorDataList(Map<String, Object> params) {
        List<Object> list = new ArrayList<Object>();
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.monitor.model.TtPackCurrent.getMonitorDataList", params);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return list;
    }

    /** (non-Javadoc)
     * <p>Title: getBatteryDetailInfo</p>
     * <p>Description: 获取电池详细信息</p>
     * @param params
     * @return
     * @see com.broadtext.app.monitor.service.MonitorService#getBatteryDetailInfo(java.util.Map)
     */
    @Override
    public Map<String, Object> getBatteryDetailInfo(Map<String, Object> params) {
        TtPackCurrent t = new TtPackCurrent();
        t.setPackCode(params.get("packCode")+"");
        t.setColClientId(params.get("colClientId")+"");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map = this.ttPackDao.getMonitorDataInfoCurrent(
                    "com.broadtext.rms.monitor.model.TtPackCurrent.getMonitorDataInfoCurrent", t);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return map;
    }

    /** (non-Javadoc)
     * <p>Title: getBatteryVolAndTempData</p>
     * <p>Description: 获取电池实时电压数据</p>
     * @param params
     * @return
     * @see com.broadtext.app.monitor.service.MonitorService#getBatteryVolData(java.util.List)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getBatteryVolData(Map<String, Object> params) {
        //存放电压信息list
        List<Object> volData = new ArrayList<Object>();
        
        try {
            //单体最高电压 单体最低电压
            params.put("clientId", params.get("colClientId"));
            BigDecimal maxVol = new BigDecimal("100");
            BigDecimal minVol = new BigDecimal("0");
            List<Map<String, Object>> volStandardList = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TtPackModel.getSingleVolStandard", params);
            if(volStandardList!=null&&volStandardList.size()>0){
                Map<String, Object> volStandardMap = volStandardList.get(0);
                if(volStandardMap!=null){
                    maxVol = new BigDecimal(volStandardMap.get("singleMaxVol").toString());
                    minVol = new BigDecimal(volStandardMap.get("singleMinVol").toString());
                }
            }
            List<String> groupNoList = this.ttPackDao.getNoNums("com.broadtext.rms.monitor.model.TtCellvoltageCurrent.getRealtimeGroupNoList", params);
            if(groupNoList!=null&&groupNoList.size()>0) {
                for (int i = 0; i < groupNoList.size(); i++) {
                    Map<String, Object> module = new HashMap<String, Object>();
                    //查询实体
                    TtCellvoltageCurrent volEntity;
                    volEntity = new TtCellvoltageCurrent();
                    volEntity.setColClientId(String.valueOf(params.get("clientId")));
                    volEntity.setPackCode(String.valueOf(params.get("packCode")));
                    volEntity.setGroupNo(groupNoList.get(i));
                    
                    //电压信息list
                    List<TtCellvoltageCurrent> volList = this.entityDao.findByProperty(
                            "com.broadtext.rms.monitor.model.TtCellvoltageCurrent.getVolList", volEntity);
                    //电池单体编号信息
                    List<String> groupCoreNoList = new ArrayList<String>();
                    //单体电压信息
                    List<BigDecimal> singleVolList = new ArrayList<BigDecimal>();
                    if(volList!=null&&volList.size()>0) {
                        for (int j = 0; j < volList.size(); j++) {
                            groupCoreNoList.add(volList.get(j).getGroupCoreNo());
                            singleVolList.add(volList.get(j).getSinglecoreVoltage());
                        }
                    }
                    module.put("groupNo", groupNoList.get(i));
                    module.put("maxVol", maxVol);
                    module.put("minVol", minVol);
                    module.put("groupCoreNoList", groupCoreNoList);
                    module.put("singleVolList", singleVolList);
                    volData.add(module);
                }
            }
            
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return volData;
    }
    
    
    /** (non-Javadoc)
     * <p>Title: getBatteryVolAndTempData</p>
     * <p>Description: 获取电池实时温度数据</p>
     * @param params
     * @return
     * @see com.broadtext.app.monitor.service.MonitorService#getBatteryTempData(java.util.List)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getBatteryTempData(Map<String, Object> params) {
        //存放电压信息list
        List<Object> tempData = new ArrayList<Object>();
        try {
            List<String> groupNoList = this.ttPackDao.getNoNums("com.broadtext.rms.monitor.model.TtSamplingpointTempCurrent.getRealtimeGroupNoList", params);
            if(groupNoList!=null&&groupNoList.size()>0) {
                for (int i = 0; i < groupNoList.size(); i++) {
                    Map<String, Object> module = new HashMap<String, Object>();
                    //查询实体
                    TtCellvoltageCurrent volEntity;
                    volEntity = new TtCellvoltageCurrent();
                    volEntity.setColClientId(String.valueOf(params.get("clientId")));
                    volEntity.setPackCode(String.valueOf(params.get("packCode")));
                    volEntity.setGroupNo(groupNoList.get(i));
                    
                    //电压信息list
                    List<TtSamplingpointTempCurrent> tempList = this.entityDao.findByProperty(
                            "com.broadtext.rms.monitor.model.TtSamplingpointTempCurrent.getTempDataList", volEntity);
                    //电池单体编号信息
                    List<String> groupCoreNoList = new ArrayList<String>();
                    //单体电压信息
                    List<BigDecimal> samplingpointTempList = new ArrayList<BigDecimal>();
                    if(tempList!=null&&tempList.size()>0) {
                        for (int j = 0; j < tempList.size(); j++) {
                            groupCoreNoList.add(tempList.get(j).getSamplingpointNo());
                            samplingpointTempList.add(tempList.get(j).getSamplingpointTemp());
                        }
                    }
                    module.put("groupNo", groupNoList.get(i));
                    module.put("groupCoreNoList", groupCoreNoList);
                    module.put("samplingpointTempList", samplingpointTempList);
                    tempData.add(module);
                }
            }
            
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return tempData;
    }

    /** (non-Javadoc)
     * <p>Title: getCustomerOptions</p>
     * <p>Description: </p>
     * @param params
     * @return
     * @see com.broadtext.app.monitor.service.MonitorService#getCustomerOptions(java.util.Map)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getCustomerOptions(Map<String, Object> params) {
        List<Object> list = new ArrayList<Object>();
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.getAllCustomers", null);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return list;
    }
    

}
